//
//  YBSMSWaterLayoutView.m
//  GongXinDangJian
//
//  Created by apple on 17/12/7.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "YBSMSWaterLayoutView.h"
#import "YBWaterLayout.h"
#import "YBNormalWaterCollectionViewCell.h"
#define CELLID @"YBNormalWaterCollectionViewCell"

@interface YBSMSWaterLayoutView ()<UICollectionViewDelegate , UICollectionViewDataSource ,YBWaterLayoutDelegate>

@end

@implementation YBSMSWaterLayoutView

- (instancetype)initWithFrame:(CGRect)frame title:(NSString *)title
{
    self = [super initWithFrame:frame];
    if (self) {
        self.dataArray = @[];
        self.titleLabel = [ToolClass creatLabelWithFrame:CGRectMake(ItemWidth * 30, 0, [UIScreen zj_screenWith] - ItemWidth * 60, ItemHeight * 80) font:15 textColor:[UIColor textBlackColor] text:title];
        [self addSubview:self.titleLabel];
        [self ybInitMyView];
    }
    return self;
}

-(void)ybInitMyView{
    [self addSubview:self.collectionView];
    
    CGFloat width = [@"定时发送:" boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : LYPFONT(15)} context:nil].size.width;
    
    UILabel * sendLabel = [ToolClass creatLabelWithFrame:CGRectMake(ItemWidth * 30, ItemHeight * 320, width + 1, ItemHeight * 80) font:15 textColor:[UIColor textBlackColor] text:@"定时发送:"];
    [self addSubview:sendLabel];
    
    [self addSubview:self.selectButton];
    
    self.selectButton.sd_layout.leftSpaceToView(sendLabel , ItemWidth * 5).centerYEqualToView(sendLabel).widthIs(ItemWidth * 100).heightIs(ItemHeight * 80);
    
    [self addSubview:self.dateInputTextField];
    
}


-(void)option_flowLayoutCellDeleteButton:(UIButton *)sender{
    NSInteger index = sender.tag - 500;
    if (self.delegate && [self.delegate respondsToSelector:@selector(YBSMSWaterLayoutViewDidClickDeleteButtonWithIndex:)]) {
        [self.delegate YBSMSWaterLayoutViewDidClickDeleteButtonWithIndex:index];
    }
}


-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataArray.count;
}


-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    YBNormalWaterCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:CELLID forIndexPath:indexPath];
    cell.model = self.dataArray[indexPath.row];
    cell.deleteButton.tag = 500 + indexPath.row;
    [cell.deleteButton addTarget:self action:@selector(option_flowLayoutCellDeleteButton:) forControlEvents:UIControlEventTouchUpInside];
    return cell;
}


-(CGFloat)WaterfallFlowLayout:(YBWaterLayout *)layout widthForItemAtIndexPath:(NSIndexPath *)indexPath
{
    YBPeopleInfoModel * model = self.dataArray[indexPath.row];
    return model.nameWidth + ItemWidth * 60;
}

-(void)setDataArray:(NSArray *)dataArray
{
    _dataArray = dataArray;
    self.titleLabel.text = [NSString stringWithFormat:@"发布对象(%ld):" , dataArray.count];
    [self.collectionView reloadData];
}



-(UICollectionView *)collectionView
{
    if (!_collectionView) {
        YBWaterLayout * layout = [[YBWaterLayout alloc]init];
        layout.minimumLineSpacing = ItemHeight * 1;
        layout.minimumInteritemSpacing = ItemWidth * 10;
        layout.sectionInset = UIEdgeInsetsMake(ItemHeight * 5, ItemWidth * 10, ItemHeight * 5 , ItemWidth * 10);
        layout.itemHeight = 75 * ItemHeight;
        layout.delegate = self;
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(ItemWidth * 30, ItemHeight * 80, [UIScreen zj_screenWith] - ItemWidth * 60, ItemHeight * 240) collectionViewLayout:layout];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.layer.borderColor = [UIColor textGrayColor].CGColor;
        _collectionView.layer.borderWidth = 1;
        _collectionView.clipsToBounds = YES;
        _collectionView.layer.cornerRadius = normalCornerRadius;
        [_collectionView registerClass:[YBNormalWaterCollectionViewCell class] forCellWithReuseIdentifier:CELLID];
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.allowsSelection = NO;
    }
    return _collectionView;
}


-(UIButton *)selectButton
{
    if (!_selectButton) {
        _selectButton = [ToolClass createButtonWithFrame:CGRectMake(0, 0, 1, 1) text:@"" textColor:[UIColor whiteColor] imageName:@"list_select" font:15];
        [_selectButton setImage:[UIImage imageNamed:@"list_selected"] forState:UIControlStateSelected];
        _selectButton.imageEdgeInsets = UIEdgeInsetsMake((ItemHeight * 80 - ItemWidth * 42) / 2, 0, (ItemHeight * 80 - ItemWidth * 42) / 2, ItemWidth * 58);
    }
    return _selectButton;
}

-(UITextField *)dateInputTextField
{
    if (!_dateInputTextField) {
        _dateInputTextField = [[UITextField alloc]initWithFrame:CGRectMake(ItemWidth * 30, ItemHeight * 400, [UIScreen zj_screenWith] - ItemWidth * 60, ItemHeight * 60)];
        UIView * tempView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ItemWidth * 80, ItemHeight * 60)];
        UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, (ItemHeight * 60 - ItemWidth * 42) / 2, ItemWidth * 42, ItemWidth * 42)];
        imageView.image = [UIImage imageNamed:@"footmark_add_calendar"];
        [tempView addSubview:imageView];
        _dateInputTextField.rightView = tempView;
        _dateInputTextField.rightViewMode = UITextFieldViewModeAlways;
        _dateInputTextField.font = LYPFONT(15);
        _dateInputTextField.clipsToBounds = YES;
        _dateInputTextField.layer.cornerRadius = normalCornerRadius;
        _dateInputTextField.layer.borderColor = [UIColor themeGrayColor].CGColor;
        _dateInputTextField.layer.borderWidth = 1;
        _dateInputTextField.hidden = YES;
        _dateInputTextField.returnKeyType = UIReturnKeyDone;
    }
    return _dateInputTextField;
}

@end
